﻿//-----------------------------------------------------------------------------------------------------------
// WBFSSync Project by Omega Frost 
// http://wbfssync.codeplex.com/
//
// WBFSSync is Licensed under the terms of the 
// Microsoft Reciprocal License (Ms-RL)
//-----------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WBFSe3.IO;
using System.Runtime.InteropServices;

namespace WbfsSync.Core
{
    //-------------------------------------------------------------------------------------------------------
    //
    //-------------------------------------------------------------------------------------------------------
    public static class Language
    {
        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public static String FormatSize(long size)
        {
            if (size >= 1000000000) return ((float)size / 1000000000.0f).ToString("F") + " GB";
            else if (size >= 1000000) return ((float)size / 1000000.0f).ToString("F") + " MB";
            else if (size >= 1000) return ((float)size / 1000.0f).ToString("F") + " KB";
            else return size.ToString() + " bytes";
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public static String FormatSizeEbi(long size)
        {
            if (size >= 1073741824) return ((float)size / 1073741824.0f).ToString("F") + " GB";
            else if (size >= 1048576) return ((float)size / 1048576.0f).ToString("F") + " MB";
            else if (size >= 1024) return ((float)size / 1024.0f).ToString("F") + " KB";
            else return size.ToString() + " bytes";
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public static String GetDriveString(WbfsDriveInfo drive)
        {
            String text;
            if (String.IsNullOrEmpty(drive.Label))
                text = GetDriveType(drive);
            else
                text = drive.Label;

            return String.Format("{0} ({1}:)", text, drive.Name[0]);
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public static string GetDriveType(WbfsDriveInfo drive)
        {
            if (drive.IsWbfs) return "Wbfs Disk";
            else if(drive.IsFatWbfs) return "Fat-Wbfs Disk";
            else if (drive.IsCd) return "CD Drive";
            else if (drive.IsFixed) return "Local Disk";
            else return "Removable Disk";
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public static string GetDriveGroup(WbfsDriveInfo drive)
        {
            if (drive.IsFixed && !drive.IsCd) return "Hard Disk Drives";
            else return "Devices with Removable Storage";
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public static String GetErrorString(int error)
        {
            if (error > 0)
            {
                String sMessage = String.Empty;
                IntPtr pMessage = IntPtr.Zero;
                uint dwChars;

                dwChars = Native.FormatMessage(0x00000100 | 0x00001000 | 0x00000200,
                    IntPtr.Zero, (uint)error, 0, ref pMessage, 0, IntPtr.Zero);

                if (dwChars == 0)
                    return "No message";

                sMessage = Marshal.PtrToStringAnsi(pMessage);
                Native.LocalFree(pMessage);
                return sMessage;
            }
            else
            {
                return "No message";
            }
        }
    }
}
